<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleForm({})" v-auth="'mo:error:mesLineError:edit'">
          <Icon icon="ant-design:bug-outlined" /> {{ t('异常上报') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.basLine.name }}
        </a>
      </template>
      <template #uploadFile="{ record }">
        <!--    v-if="uploadshow" -->
        <BasicUpload
          v-model:value="record.dataMap"
          disabled
          :bizKey="record.id"
          :bizType="'line_error_file'"
          :uploadType="'image'"
          :loadTime="record.__t"
          :showPreview="true"
          :isPreview="false"
          :emptyHidePreview="true"
        />
      </template>

    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <DealForm @register="registerDealDrawer" @success="handleSuccess" />
    <BpmRuntimeTrace @register="registerTraceModal" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsMoErrorMesLineErrorList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { mesLineErrorDelete, mesLineErrorListData } from '/@/api/mes/mo/error/mesLineError';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import { useModal } from '/@/components/Modal';
  import { BpmRuntimeTrace } from '/@/components/Bpm';
  import InputForm from './form.vue';
  import DealForm from './dealForm.vue';
  import { registerCoordinateSystem } from 'echarts';
  import { BasicUpload } from '/@/components/Upload';

  const { t } = useI18n('mo.error.mesLineError');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('异常上报管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('机台'),
        field: 'basLine.name',
        component: 'Input',
      },
      {
        label: t('异常类型'),
        field: 'errorType',
        component: 'Select',
        componentProps: {
          dictType: 'mes_error_type',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('上报人'),
        field: 'createByName',
        component: 'Input',
      },
      {
        label: t('发生时间'),
        field: 'createDate',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          showTime: false,
        },
      },
      {
        label: t('处理人'),
        field: 'dealUserName',
        component: 'Input',
      },
      {
        label: t('处理时间'),
        field: 'dealDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          showTime: false,
        },
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          // dictType: '	mes_error_status',
          dictType: '	bpm_biz_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
    fieldMapToTime: [['createDate', ['createDate_gte', 'createDate_lte']]],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('机台'),
      dataIndex: 'lineCode',
      key: 'a.line_code',
      sorter: true,
      width: 100,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('异常类型'),
      dataIndex: 'errorType',
      key: 'a.error_type',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'mes_error_type',
    },
    {
      title: t('异常描述'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('上报人'),
      dataIndex: 'createByName',
      key: 'a.create_by_name',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('发生时间'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('处理人'),
      dataIndex: 'dealUserName',
      key: 'a.deal_user_name',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('处理时间'),
      dataIndex: 'dealDate',
      key: 'a.deal_date',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('处理结果'),
      dataIndex: 'dealResult',
      key: 'a.deal_result',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('图片'),
      dataIndex: 'tu',
      width: 80,
      align: 'left',
      slot: 'uploadFile',
      fixed: 'right',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 100,
      align: 'center',
      // dictType: 'mes_error_status',
      dictType: 'bpm_biz_status',
      fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑异常上报'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'mo:error:mesLineError:edit',
        ifShow: record.status === '9',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除异常上报'),
        popConfirm: {
          title: t('是否确认删除异常上报'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'mo:error:mesLineError:edit',
        ifShow: record.status === '9',
      },
      
      {
        icon: 'fluent:flowchart-20-regular',
        title: t('流程追踪'),
        onClick: handleTrace.bind(this, record),
        ifShow: () => record.status != '9',
      },
      {
        label: t('异常处理'),
        title: t('异常处理'),
        onClick: handleDealForm.bind(this, { id: record.id }),
        auth: 'mo:error:mesLineError:error',
        // ifShow: record.status === '0',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerDealDrawer, { openDrawer: openDealDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRowKeys }] = useTable({
    api: mesLineErrorListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handleDealForm(record: Recordable) {
    openDealDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await mesLineErrorDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  const [registerTraceModal, { openModal: traceModel }] = useModal();

  function handleTrace(record: Recordable) {
    traceModel(true, { formKey: 'mes_Line_Error', bizKey: record.id });
  }
</script>
